﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Payton.WP7.Common.Model;
using Newtonsoft.Json.Linq;
using Payton.WP7.Common.Interface;
using System.Globalization;
using System.Diagnostics;
using Payton.WP7.Common.Renren;

namespace Payton.WP7.Common.Renren
{
    public class RenrenJTokenToUserMapping : MappingBase<JToken, SNSUser>
    {
        public RenrenJTokenToUserMapping(IMappingFactory factory)
            : base(factory)
        {

        }

        public override SNSUser Map(JToken s)
        {
            try
            {
                var user = new SNSUser();
                if (s.SelectToken("uid") != null)
                    user.Id = (long)s.SelectToken("uid");
                else if (s.SelectToken("actor_id") != null)
                    user.Id = (long)s.SelectToken("actor_id");
                //TODO: Account
                //user.Account = _factory.Account;
                user.AccountID = _factory.Account.AccountID;
                user.AccountName = (string)s.SelectToken("name");
                user.Name = (string)s.SelectToken("name");
                //if (s.SelectToken("actor_type") != null)
                //    user.ActorType = (string)s.SelectToken("actor_type");
                //user.Province = int.Parse((string)s.SelectToken("province"));
                //user.City = int.Parse((string)s.SelectToken("city"));
                //user.Location = (string)s.SelectToken("location");
                //user.Description = (string)s.SelectToken("description");
                //user.Url = (string)s.SelectToken("url");
                user.ProfileImageUrl = (string)s.SelectToken("headurl");
                //user.Domain = (string)s.SelectToken("domain");
                if (s.SelectToken("sex") != null)
                    user.Gender = (int)s.SelectToken("sex") == 0 ? "女" : "男";

                //user.FollowersCount = (int)s.SelectToken("followers_count");
                //user.FriendsCount = (int)s.SelectToken("friends_count");
                //user.StatusesCount = (int)s.SelectToken("statuses_count");
                //user.FavouritesCount = (int)s.SelectToken("favourites_count");
                //user.Following = (bool)s.SelectToken("following");
                //user.Verified = (bool)s.SelectToken("verified");
                //if (s.SelectToken("status") != null)
                //    user.LastStatusText = (string)s.SelectToken("status").SelectToken("text");
                return user;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
                return null;
                //throw;
            }

            //throw new NotImplementedException();
        }
    }
}
